package com.googlecode.vmock.matchers;

import com.googlecode.vmock.matchers.ranking.MatcherRanking;
import com.googlecode.vmock.utils.Validate;

/**
 * A Matcher that matches all the values of a given type.
 * 
 * @author marcin.jagus
 */
public class TypeMatcher extends AbstractMatcher {

    static {
        MatcherRanking.setRanking(TypeMatcher.class, MatcherRanking.TYPE_MATCHER_DEFAULT_RANKING);
    }

    /**
     * Accepted type.
     */
    private Class<?> clazz;

    /**
     * Creates a new matcher for a given type.
     * 
     * @param clazz Given type
     */
    public TypeMatcher(Class<?> clazz) {
        Validate.notNull(clazz, "clazz == null");
        this.clazz = clazz;
    }

    /**
     * Returns <code>true</code> if a given value is either <code>null</code> or a non-null value of accepted type.
     */
    @Override
    public boolean matches(Object value) {
        return value == null || clazz.isAssignableFrom(value.getClass());
    }

    @Override
    public String toString() {
        return String.format("TypeMatcher(%s)", clazz.getSimpleName());
    }
}
